package org.jeecgframework.core.util.clean;

import java.io.StringReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import jeecg.business.entity.site.SiteEntity;

import org.jeecgframework.core.util.DataUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class PeopleHtmlCleanUtil {

	public static String PEOPLE_URL = "http://search.people.com.cn/rmw/GB/rmwsearch/gj_searchht.jsp";

	public static String where = "(CONTENT=({keyword}) or TITLE=({keyword}) or AUTHOR=({keyword}))";

	public static HtmlParseResult fetchPeople4Html(String keyword , String charset , SiteEntity site , String cookies){
		HtmlParseResult result = null;
		try{
			result = new HtmlParseResult();
			
			String keywordStr = keyword;
			
			keywordStr = StringUtil.removeMoreBlankLeafOne(keywordStr).trim();
			keywordStr = keywordStr.replace(" ", " AND ");
			
			
			Map<String , String> params = new HashMap<String , String>();
			params.put("basenames", "rmwsite");
			params.put("where", where.replace("{keyword}", keywordStr));
			params.put("curpage", "1");
			params.put("pagecount", "100");
			params.put("classvalue", "ALL");
			params.put("classfield", "CLASS2");
			params.put("keyword", keyword);
			params.put("sortfield", "LIFO");
			
			String html = HttpClientUtil.postSourceFromUrl(PEOPLE_URL, charset, null, params, null);
			
			html = html.replace("{xml version='1.0' encoding='UTF-8' ?>", "");
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource();  
	        is.setCharacterStream(new StringReader(html));
	        Document doc = db.parse(is);
	        NodeList nodes = doc.getElementsByTagName("DOCURL");
	        NodeList times = doc.getElementsByTagName("PUBLISHTIME");
	        if(nodes != null && nodes.getLength()>0){
	        	Node node = null;
	        	Node timenode = null;
	        	String url = null;
	        	String time = null;
	        	HtmlParseResult each = null;
	        	Date date = null;
	        	for(int i=0;i<nodes.getLength();i++){
	        		node = nodes.item(i);
	        		timenode = times.item(i);
	        		url = node.getTextContent();
	        		time = timenode.getTextContent();
	        		url = url.replace("<![CDATA[", "").replace("]]>", "");
	        		time = time.replace("<![CDATA[", "").replace("]]>", "");
	        		date = DataUtils.parseDate(time, "yyyy年MM月dd日HH时mm分ss秒");
					each = new HtmlParseResult();
		            each.setType(site.getJclx());
		            each.setUrl(url);
		            each.setEventDate(date);
		            result.addChildResult(each);
	        	}
	        }
//			System.out.println(nodes.getLength());
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return result;
	}
	
	public static void main(String[] args) {
		String s = "  A   B     C     D E F G";
		System.out.println(s.replaceAll(" +"," "));
	}

}
